/* StringDeParser.java
 * 
 * Alfred Olschnoegger
 * 09.06.2008
 */

package technical.deParser;

import java.util.Vector;

import domain.Game;
import domain.GameObject;
import domain.Hunter;
import domain.location.GameArea;
import domain.location.Position;

import exceptions.ParseException;
import exceptions.RabbitBusterException;

/**
 * TODO description
 * 
 * @date 09.06.2008
 * @author Alfred Olschnoegger
 */

public class StringDeParser extends DeParser {
	RabbitBusterException m_Exception = null;
	private boolean m_Successfull = false;
	private Vector m_Games = new Vector();
	private Vector m_GameObjects = new Vector();
	private String m_Version;
	private String m_AuthKey;
	private Vector m_Hunters = new Vector();

	private void checkException() throws RabbitBusterException {
		if (m_Exception != null)
			throw m_Exception;
	}

	public StringDeParser Deparse(String input) {
		System.out.println(input);
		try {
			StringIdentifier ident = StringIdentifier.getInstance();

			for (int i = 0; i < input.length(); i++) {
				if (input.charAt(i) == ident.getExceptionValue()) {
					i++;
					String val = getNextValue(i, input);
					i = i + val.length() + 1;
					val = ident.getExceptionClassName(Integer.parseInt(val));

					m_Exception = (RabbitBusterException) Class.forName("exceptions." + val).newInstance();

				} else if (input.charAt(i) == ident.getSuccessValue()) {
					m_Successfull = true;

				} else if (input.charAt(i) == ident.getInformationValue()) {
					i++;
					
					while ((i < input.length()) && (!ident.isIdentifier(input.charAt(i)))) {
						String val = getNextValue(i, input);
						i = i + val.length() + 1;
						if (val.equals(ident.getAuthIdent())) {
							m_AuthKey = getNextValue(i, input);
							i = i + m_AuthKey.length() + 1;

						} else if (val.equals(ident.getInfoIdent())) {

							m_Version = getNextValue(i, input);
							i = i + m_Version.length() + 1;
						}
					}

				} else if (input.charAt(i) == ident.getDataValue()) {
					i++;
					
					String val;
					GameObject obj;
					Game g;
					int x, y;

					while ((i < input.length()) && (!ident.isIdentifier(input.charAt(i)))) {
						// reading object identifier
						val = getNextValue(i, input);
						i = i + val.length() + 1;

						if (val.equals(ident.getGameIdent())) {

							// creating object
							g = new Game();

							// id
							val = getNextValue(i, input);
							i = i + val.length() + 1;
							g.setGameID(Integer.parseInt(val));

							// name
							val = getNextValue(i, input);
							i = i + val.length() + 1;
							g.setName(val);

							// position
							double Xlongitude;
							double Xlatitude;
							double Ylongitude;
							double Ylatitude;
							// skip running time
							val = getNextValue(i, input);
							i = i + val.length() + 1;
							
							val = getNextValue(i, input);
							i = i + val.length() + 1;
							Xlongitude = Double.parseDouble(val);

							val = getNextValue(i, input);
							i = i + val.length() + 1;
							Xlatitude = Double.parseDouble(val);

							val = getNextValue(i, input);
							i = i + val.length() + 1;
							Ylongitude = Double.parseDouble(val);

							val = getNextValue(i, input);
							i = i + val.length() + 1;
							Ylatitude = Double.parseDouble(val);


							g.setArea(new GameArea(Xlongitude, Xlatitude, Ylongitude, Ylatitude));
							
							m_Games.addElement(g);

						} else if (val.equals(ident.getHunterIdent())){
							// creating object
							Hunter h = new Hunter();

							// username
							val = getNextValue(i, input);
							i = i + val.length() + 1;
							h.setName(val);
							
							// kills
							val = getNextValue(i, input);
							i = i + val.length() + 1;
							h.setKills(Integer.parseInt(val));
							
							// x
							val = getNextValue(i, input);
							i = i + val.length() + 1;
							x = Integer.parseInt(val);
							
							// y
							val = getNextValue(i, input);
							i = i + val.length() + 1;
							y = Integer.parseInt(val);
							
							h.setPos(new Position(x,y));
							
							m_Hunters.addElement(h);
							
						} else {
							// creating object
							val = ident.getObjectClassName(val);
							Class tmpObj =  Class.forName("domain." + val);
							obj = (GameObject) tmpObj.newInstance();
							// reading id
							val = getNextValue(i, input);
							i = i + val.length() + 1;
							obj.setID(Integer.parseInt(val));

							// reading x value
							val = getNextValue(i, input);
							i = i + val.length() + 1;
							x = Integer.parseInt(val);

							// reading y value
							val = getNextValue(i, input);
							i = i + val.length() + 1;
							y = Integer.parseInt(val);

							// setting position
							obj.setPos(new Position(x, y));
							
							m_GameObjects.addElement(obj);

						}
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			m_Exception = new ParseException();
		}
		System.out.println("deparseEnd");

		return this;
	}

	public RabbitBusterException getException() {
		return m_Exception;
	}

	public Vector getGameObjects() throws RabbitBusterException {
		checkException();
		return m_GameObjects;
	}

	public Vector getGames() throws RabbitBusterException {
		checkException();
		return m_Games;
	}

	public String getVersion() throws RabbitBusterException {
		checkException();
		return m_Version;
	}

	public long getAuthKey() throws RabbitBusterException {
		checkException();
		return Long.parseLong(m_AuthKey);
	}

	public boolean isSuccessfull() throws RabbitBusterException {
		checkException();
		return m_Successfull;
	}

	private String getNextValue(int from, String str) {
		StringBuffer tmp = new StringBuffer();
		StringIdentifier ident = StringIdentifier.getInstance();
		while (str.charAt(from) != ident.getFieldSeperator() && str.charAt(from) != ident.getRecordSeperator()) {
			tmp.append(str.charAt(from));
			from++;
		}

		return tmp.toString();
	}

	public Vector getHunters() throws RabbitBusterException {
		checkException();
		return m_Hunters;
	}

}

// end of StringDeParser.java
